
package org.vasil.jouikov.service

class HttpSend {

    def sendXml = { xmlbody ->

        try {
            def connection = "http://localhost:8080/Synchro/items".toURL().openConnection()
            connection.setRequestProperty("Content-Type", "application/xml")
            connection.setRequestMethod("POST")
            connection.doOutput = true

            connection.outputStream.withWriter { writer ->  writer << xmlbody }

            def response = connection.inputStream.withReader { reader -> reader.text }
            
            if(connection.responseCode == 200 || connection.responseCode == 201) {
                return [status: "OK", body: response]
            }
            return [status: "ERR", body: "${connection.responseCode} : ${connection.responseMessage}"]
        }
        catch(error) {
            return [status: "ERR", body: error]
        }
    }
}

